import { _decorator, Node } from 'cc';
import { ListRender } from '../../Basic/Component/ListRender';
import { autoNode } from '../../Basic/Manager/BaseManager';
import { MainModel_Ins } from './MainModel';
import { AudioManager } from '../../Basic/Manager/AudioManager';
import { SoundCode } from '../../Basic/Config/BaseEnum';
import { WeaponCreateRate } from '../Config/ConstData';

const { ccclass, property } = _decorator;
@ccclass('TreeItem')
export class TreeItem extends ListRender {
  @autoNode()
  tree_1: Node;
  @autoNode()
  tree_2: Node;
  data: number[];
  dataChanged(...args: unknown[]): void {
    this.tree_1.active = this.data[0] === 1;
    this.tree_2.active = this.data[0] === 2;
  }

  hirt() {
    if (this.data[0] <= 0) return;
    AudioManager.Singleton.playSound(SoundCode.Tree);

    this.data[0]--;
    this.dataChanged();
    MainModel_Ins.basePanel.playAniEff(this.data[0] === 1 ? 'pre_eff_tree_2' : 'pre_eff_tree_1', this.node.getPosition());

    MainModel_Ins.basePanel.createDropItem('pre_eff_wood', this.node.getWorldPosition(), () => MainModel_Ins.basePanel.addCoin());

    if (this.data[0] <= 0 && Math.random() < WeaponCreateRate) {
      MainModel_Ins.basePanel.createWeapon(this.node.getPosition());
    }
  }
}
